package com.jingwu.example.graphql;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.coxautodev.graphql.tools.GraphQLQueryResolver;
import com.jingwu.example.pojo.entity.AuthorDO;
import com.jingwu.example.pojo.entity.CategoryDO;
import com.jingwu.example.pojo.query.AuthorQuery;
import com.jingwu.example.pojo.query.CategoryQuery;
import com.jingwu.example.pojo.vo.AuthorVO;
import com.jingwu.example.pojo.vo.CategoryVO;
import com.jingwu.example.pojo.vo.Pagination;
import com.jingwu.example.repo.AuthorRepository;
import com.jingwu.example.repo.CategoryRepository;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.deepexi.web.support.pojo.converter.utils.ConverterUtils.convert;
import static com.deepexi.web.support.pojo.converter.utils.ConverterUtils.convertAll;

/**
 * @author jingwu
 * @since 2021/2/3
 */

@Service
public class AuthorQueryResolver implements GraphQLQueryResolver {

    private final AuthorRepository repository;

    public AuthorQueryResolver(AuthorRepository repository) {
        this.repository = repository;
    }

    public List<AuthorVO> listAuthors(AuthorQuery query) {
        List<AuthorDO> list = repository.select(query);
        return convertAll(list, AuthorVO.class);
    }

    public Pagination<AuthorVO> getAuthorPage(AuthorQuery query) {
        Page<AuthorDO> page = repository.selectPage(query);
        return new Pagination<>(page.getTotal(), convertAll(page.getRecords(), AuthorVO.class));
    }

    public AuthorVO getAuthor(Integer id) {
        AuthorDO entity = repository.selectById(id);
        return convert(entity, AuthorVO.class);
    }
}
